import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { policeStateType } from './typings'
import {
  alarmInfoList,
  alarmInfoExport,
  alarmInfoView,
  ignoreAlarm,
  pushOrder,
  pushAnyOrder,
  selectAlarmBoxList,
  selectAreaNameBoxList,
  historyAlarmList,
  analysAlarmLimit,
  analysAlarmDate,
  alarmConfigListGet,
  alarmConfigUpdatePut
} from '~/api/monitor/police'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum, DownloadAttachment } from '~/utils/util'

const police: Module<policeStateType, policeStateType> = {
  namespaced: true,
  state: {
    //  报警名称
    alarmNameOpt: [],
    //  所属工区
    workAreaOpt: [],
    //  报警状态
    alarmStatusOpt: [],
    alarmStatusEnum: {},
    //  忽略状态
    ignoreStatusOpt: [],
    ignoreStatusEnum: {}
  },
  getters: {
    alarmNameOpt: (state) => state.alarmNameOpt,
    workAreaOpt: (state) => state.workAreaOpt,
    alarmStatusOpt: (state) => state.alarmStatusOpt,
    alarmStatusEnum: (state) => state.alarmStatusEnum,
    ignoreStatusOpt: (state) => state.ignoreStatusOpt,
    ignoreStatusEnum: (state) => state.ignoreStatusEnum
  },
  mutations: {
    SET_ALARMNAME(state, payload) {
      state.alarmNameOpt = payload
    },
    SET_ALARMSTATUS(state, payload) {
      state.alarmStatusOpt = payload
      state.alarmStatusEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    },
    SET_IGNORESTATUS(state, payload) {
      state.ignoreStatusOpt = payload
      state.ignoreStatusEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    },
    SET_WORKAREA(state, payload) {
      state.workAreaOpt = payload
    }
  },
  actions: {
    //  报警数据-列表
    AlarmInfoList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        alarmInfoList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警数据-详情
    AlarmInfoView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        alarmInfoView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警数据-历史报警
    HistoryAlarmList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        historyAlarmList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警数据-忽略
    IgnoreAlarm<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        ignoreAlarm<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警数据-推送工单
    PushOrder<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        pushOrder<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警数据-推送工单
    PushAnyOrder<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        pushAnyOrder<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警分析-数量
    AnalysAlarmLimit<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        analysAlarmLimit<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警分析-时间
    AnalysAlarmDate<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        analysAlarmDate<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  查询警报设置列表
    AlarmConfigListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        alarmConfigListGet<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  查询警报设置列表
    AlarmConfigUpdatePut<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        alarmConfigUpdatePut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警状态
    AlarmStatusOpt<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDataDictionaryByCode<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_ALARMSTATUS', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  忽略状态
    IgnoreStatusOpt<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDataDictionaryByCode<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_IGNORESTATUS', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  报警名称
    SelectAlarmBoxList<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        selectAlarmBoxList<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_ALARMNAME', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  所属工区
    SelectAreaNameBoxList<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        selectAreaNameBoxList<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_WORKAREA', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 报警数据-导出
    async AlarmInfoExport<S, T>(_: ActionContext<S, T>, params: T) {
      return await alarmInfoExport<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    }
  }
}

export default police
